//进行经纬度转换为距离的计算
function Rad(d: any): number {
    return (d * Math.PI) / 180.0; //经纬度转换成三角函数中度分表形式。
}

/*
计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
默认单位km
*/
export const getMapDistance = (lat1: any, lng1: any, lat2: any, lng2: any): number => {
    var radLat1 = Rad(lat1);
    var radLat2 = Rad(lat2);
    var a = radLat1 - radLat2;
    var b = Rad(lng1) - Rad(lng2);
    var s =
        2 *
        Math.asin(
            Math.sqrt(
                Math.pow(Math.sin(a / 2), 2) +
                Math.cos(radLat1) *
                Math.cos(radLat2) *
                Math.pow(Math.sin(b / 2), 2)
            )
        );
    s = s * 6378.137; // EARTH_RADIUS;
    s = Math.round(s * 10000) / 10000; //输出为公里
    //s=s.toFixed(2);
    return s;
}

//根据身份证计算出生日期
export const getBirthDayByIdCard = (idCard: string): string => {
    return idCard.substring(6, 10) + '-' + idCard.substring(10, 12) + '-' + idCard.substring(12, 14);
}
export const getAgeByIdCard = (idCard: string): number => {
    var year: number = parseInt(idCard.substring(6, 10));
    var month = parseInt(idCard.substring(10, 12));
    var day = parseInt(idCard.substring(12, 14));
    var date = new Date()
    var nowMonth = date.getMonth() + 1;
    var nowDate = date.getDate();
    var age = date.getFullYear() - year - 1;
    if (nowMonth > month || (nowMonth == month && nowDate >= day)) {
        age += 1;
    }
    return age;
}
export const getSexByIdCard = (idCard: string): string => {
    if (parseInt(idCard.substring(16, 17)) % 2 == 1) {
        return '男';
    } else {
        return '女';
    }
}
//格式化Number 日期
export const getFormatDate_XLSX = (serial: number): Date => {
    var utc_days = Math.floor(serial - 25569);
    var utc_value = utc_days * 86400;
    var date_info = new Date(utc_value * 1000);
    var fractional_day = serial - Math.floor(serial) + 0.0000001;
    var total_seconds = Math.floor(86400 * fractional_day);
    var seconds = total_seconds % 60;
    total_seconds -= seconds;
    var hours = Math.floor(total_seconds / (60 * 60));
    var minutes = Math.floor(total_seconds / 60) % 60;
    var d = new Date(
        date_info.getFullYear(),
        date_info.getMonth(),
        date_info.getDate(),
        hours,
        minutes,
        seconds
    );
    return d;
}
/**
 * 解析url参数
 * @param  url  
 */
export const parseQueryParam = (url: string): any => {
    var params: any = {};
    var arr = url.split("?");
    if (arr.length <= 1) {
        return params;
    }
    arr = arr[1].split("&");
    for (var i = 0, l = arr.length; i < l; i++) {
        var a = arr[i].split("=");
        params[a[0]] = a[1];
    }
    return params;
}

export const Storage = {
    set: function (key: string, value: any, ttl_ms?: any) {
        ttl_ms || (ttl_ms = new Date().getTime() + 1000 * 60 * 600)
        let data = { value: value, expirse: new Date(ttl_ms).getTime() };
        localStorage.setItem(key, JSON.stringify(data));
    },
    get: function (key: string) {
        let data = JSON.parse(<string>localStorage.getItem(key));
        if (data !== null) {
            return data.value;
        }
        return null
    }
}

export function hasOwnProperty<T, K extends PropertyKey>(
    obj: T,
    prop: K
): obj is T & Record<K, unknown> {
    return Object.prototype.hasOwnProperty.call(obj, prop);
}

export function setFormData(formData: Record<any, any>, data: Record<any, any>) {
    for (let k in formData) {
        if (hasOwnProperty(formData, k)) {
            formData[k] = data[k]
        }
    }
}